


<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>torch._jit_internal &mdash; PyTorch master documentation</title>
  

  
  
  
  
    <link rel="canonical" href="https://pytorch.org/docs/stable/_modules/torch/_jit_internal.html"/>
  

  

  
  
    

  

  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <!-- <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" /> -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/css/jit.css" type="text/css" />
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/katex-math.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 

  
  <script src="../../_static/js/modernizr.min.js"></script>

  <!-- Preload the theme fonts -->

<link rel="preload" href="../../_static/fonts/FreightSans/freight-sans-book.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../_static/fonts/FreightSans/freight-sans-medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../_static/fonts/IBMPlexMono/IBMPlexMono-Medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../_static/fonts/FreightSans/freight-sans-bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../_static/fonts/FreightSans/freight-sans-medium-italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../_static/fonts/IBMPlexMono/IBMPlexMono-SemiBold.woff2" as="font" type="font/woff2" crossorigin="anonymous">

<!-- Preload the katex fonts -->

<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Math-Italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Main-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Main-Bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size1-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size4-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size2-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size3-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Caligraphic-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
</head>

<div class="container-fluid header-holder tutorials-header" id="header-holder">
  <div class="container">
    <div class="header-container">
      <a class="header-logo" href="https://pytorch.org/" aria-label="PyTorch"></a>

      <div class="main-menu">
        <ul>
          <li>
            <a href="https://pytorch.org/get-started">Get Started</a>
          </li>

          <li>
            <div class="ecosystem-dropdown">
              <a id="dropdownMenuButton" data-toggle="ecosystem-dropdown">
                Ecosystem
              </a>
              <div class="ecosystem-dropdown-menu">
                <a class="nav-dropdown-item" href="https://pytorch.org/hub"">
                  <span class=dropdown-title>Models (Beta)</span>
                  <p>Discover, publish, and reuse pre-trained models</p>
                </a>
                <a class="nav-dropdown-item" href="https://pytorch.org/ecosystem">
                  <span class=dropdown-title>Tools & Libraries</span>
                  <p>Explore the ecosystem of tools and libraries</p>
                </a>
              </div>
            </div>
          </li>

          <li>
            <a href="https://pytorch.org/mobile">Mobile</a>
          </li>

          <li>
            <a href="https://pytorch.org/blog/">Blog</a>
          </li>

          <li>
            <a href="https://pytorch.org/tutorials">Tutorials</a>
          </li>

          <li class="active">
            <a href="https://pytorch.org/docs/stable/index.html">Docs</a>
          </li>

          <li>
            <div class="resources-dropdown">
              <a id="resourcesDropdownButton" data-toggle="resources-dropdown">
                Resources
              </a>
              <div class="resources-dropdown-menu">
                <a class="nav-dropdown-item" href="https://pytorch.org/resources"">
                  <span class=dropdown-title>Developer Resources</span>
                  <p>Find resources and get questions answered</p>
                </a>
                <a class="nav-dropdown-item" href="https://pytorch.org/features">
                  <span class=dropdown-title>About</span>
                  <p>Learn about PyTorch’s features and capabilities</p>
                </a>
              </div>
            </div>
          </li>

          <li>
            <a href="https://github.com/pytorch/pytorch">Github</a>
          </li>
        </ul>
      </div>

      <a class="main-menu-open-button" href="#" data-behavior="open-mobile-menu"></a>
    </div>

  </div>
</div>


<body class="pytorch-body">

   

    

    <div class="table-of-contents-link-wrapper">
      <span>Table of Contents</span>
      <a href="#" class="toggle-table-of-contents" data-behavior="toggle-table-of-contents"></a>
    </div>

    <nav data-toggle="wy-nav-shift" class="pytorch-left-menu" id="pytorch-left-menu">
      <div class="pytorch-side-scroll">
        <div class="pytorch-menu pytorch-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          <div class="pytorch-left-menu-search">
            

            
              
              
                <div class="version">
                  master (1.5.0 )
                </div>
              
            

            


  


<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search Docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

            
          </div>

          
<div>
  <a style="color:#F05732" href="https://pytorch.org/docs/stable/_modules/torch/_jit_internal.html">
    You are viewing unstable developer preview docs.
    Click here to view docs for latest stable release.
  </a>
</div>

            
            
              
            
            
              <p class="caption"><span class="caption-text">Notes</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../notes/amp_examples.html">Automatic Mixed Precision examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notes/autograd.html">Autograd mechanics</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notes/broadcasting.html">Broadcasting semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notes/cpu_threading_torchscript_inference.html">CPU threading and TorchScript inference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notes/cuda.html">CUDA semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notes/ddp.html">Distributed Data Parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notes/extending.html">Extending PyTorch</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notes/faq.html">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notes/large_scale_deployments.html">Features for large-scale deployments</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notes/multiprocessing.html">Multiprocessing best practices</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notes/randomness.html">Reproducibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notes/serialization.html">Serialization semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notes/windows.html">Windows FAQ</a></li>
</ul>
<p class="caption"><span class="caption-text">Language Bindings</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/cppdocs/">C++ API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../packages.html">Javadoc</a></li>
</ul>
<p class="caption"><span class="caption-text">Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../torch.html">torch</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../nn.html">torch.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../nn.functional.html">torch.nn.functional</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tensors.html">torch.Tensor</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tensor_attributes.html">Tensor Attributes</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tensor_view.html">Tensor Views</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../autograd.html">torch.autograd</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../cuda.html">torch.cuda</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../amp.html">torch.cuda.amp</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../distributed.html">torch.distributed</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../distributions.html">torch.distributions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../hub.html">torch.hub</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../jit.html">torch.jit</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../nn.init.html">torch.nn.init</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../onnx.html">torch.onnx</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../optim.html">torch.optim</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../quantization.html">Quantization</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../rpc/index.html">Distributed RPC Framework</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../random.html">torch.random</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../sparse.html">torch.sparse</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../storage.html">torch.Storage</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../bottleneck.html">torch.utils.bottleneck</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../checkpoint.html">torch.utils.checkpoint</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../cpp_extension.html">torch.utils.cpp_extension</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../data.html">torch.utils.data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../dlpack.html">torch.utils.dlpack</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../model_zoo.html">torch.utils.model_zoo</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tensorboard.html">torch.utils.tensorboard</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../type_info.html">Type Info</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../named_tensor.html">Named Tensors</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../name_inference.html">Named Tensors operator coverage</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../__config__.html">torch.__config__</a></li>
</ul>
<p class="caption"><span class="caption-text">Libraries</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/audio">torchaudio</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/text">torchtext</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/elastic/">TorchElastic</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/serve">TorchServe</a></li>
<li class="toctree-l1"><a class="reference external" href="http://pytorch.org/xla/">PyTorch on XLA Devices</a></li>
</ul>
<p class="caption"><span class="caption-text">Community</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../community/contribution_guide.html">PyTorch Contribution Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../community/governance.html">PyTorch Governance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../community/persons_of_interest.html">PyTorch Governance | Persons of Interest</a></li>
</ul>

            
          

        </div>
      </div>
    </nav>

    <div class="pytorch-container">
      <div class="pytorch-page-level-bar" id="pytorch-page-level-bar">
        <div class="pytorch-breadcrumbs-wrapper">
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="pytorch-breadcrumbs">
    
      <li>
        <a href="../../index.html">
          
            Docs
          
        </a> &gt;
      </li>

        
          <li><a href="../index.html">Module code</a> &gt;</li>
        
          <li><a href="../torch.html">torch</a> &gt;</li>
        
      <li>torch._jit_internal</li>
    
    
      <li class="pytorch-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
</div>
        </div>

        <div class="pytorch-shortcuts-wrapper" id="pytorch-shortcuts-wrapper">
          Shortcuts
        </div>
      </div>

      <section data-toggle="wy-nav-shift" id="pytorch-content-wrap" class="pytorch-content-wrap">
        <div class="pytorch-content-left">

        
          
          <div class="rst-content">
          
            <div role="main" class="main-content" itemscope="itemscope" itemtype="http://schema.org/Article">
             <article itemprop="articleBody" id="pytorch-article" class="pytorch-article">
              
  <h1>Source code for torch._jit_internal</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">The weak_script annotation needs to be here instead of inside torch/jit/ so it</span>
<span class="sd">can be used in other places in torch/ (namely torch.nn) without running into</span>
<span class="sd">circular dependency problems</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">inspect</span>
<span class="kn">import</span> <span class="nn">weakref</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">from</span> <span class="nn">torch._six</span> <span class="kn">import</span> <span class="n">builtins</span>
<span class="kn">from</span> <span class="nn">torch._utils_internal</span> <span class="kn">import</span> <span class="n">get_source_lines_and_file</span>

<span class="c1"># Wrapper functions that can call either of 2 functions depending on a boolean</span>
<span class="c1"># argument</span>
<span class="n">boolean_dispatched</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">WeakKeyDictionary</span><span class="p">()</span>  <span class="c1"># noqa: T484</span>


<span class="k">def</span> <span class="nf">createResolutionCallbackFromEnv</span><span class="p">(</span><span class="n">lookup_base</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Creates a resolution callback that will look up qualified names in an</span>
<span class="sd">    environment, starting with `lookup_base` for the base of any qualified</span>
<span class="sd">    names, then proceeding down the lookup chain with the resolved object.</span>

<span class="sd">    You should not use this directly, it should only be used from the other</span>
<span class="sd">    createResolutionCallbackFrom* functions.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">env</span><span class="p">(</span><span class="n">qualified_name</span><span class="p">,</span> <span class="n">module</span><span class="p">):</span>
        <span class="c1"># We may need to resolve a qualified name, something like `torch.device`</span>
        <span class="c1"># or `a.b.c.d`. We first look up `torch` or `a` in the function&#39;s closed</span>
        <span class="c1"># over scope, then proceed to use the looked-up value to go down the</span>
        <span class="c1"># chain.</span>
        <span class="k">if</span> <span class="s1">&#39;.&#39;</span> <span class="ow">in</span> <span class="n">qualified_name</span><span class="p">:</span>
            <span class="n">parts</span> <span class="o">=</span> <span class="n">qualified_name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>
            <span class="n">base</span> <span class="o">=</span> <span class="n">parts</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">remainding_pieces</span> <span class="o">=</span> <span class="s1">&#39;.&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">parts</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
            <span class="n">module_value</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">module</span><span class="p">,</span> <span class="n">base</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">env</span><span class="p">(</span><span class="n">remainding_pieces</span><span class="p">,</span> <span class="n">module_value</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">module</span><span class="p">,</span> <span class="n">qualified_name</span><span class="p">)</span>

    <span class="k">return</span> <span class="k">lambda</span> <span class="n">key</span><span class="p">:</span> <span class="n">env</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">lookup_base</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">createResolutionCallbackFromFrame</span><span class="p">(</span><span class="n">frames_up</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Creates a function which, given a string variable name,</span>
<span class="sd">    returns the value of the variable in the scope of the caller of</span>
<span class="sd">    the function which called createResolutionCallbackFromFrame (by default).</span>

<span class="sd">    This is used to enable access in-scope Python variables inside</span>
<span class="sd">    TorchScript fragments.</span>

<span class="sd">    frames_up is number of additional frames to go up on the stack.</span>
<span class="sd">    The default value is 0, which correspond to the frame of the caller</span>
<span class="sd">    of createResolutionCallbackFromFrame. Also for example, if frames_up is set</span>
<span class="sd">    to 1, then the frame of the caller&#39;s caller of createResolutionCallbackFromFrame</span>
<span class="sd">    will be taken.</span>

<span class="sd">    For example, the following program prints 2::</span>

<span class="sd">        def bar():</span>
<span class="sd">            cb = createResolutionCallbackFromFrame(1)</span>
<span class="sd">            print(cb(&quot;foo&quot;))</span>

<span class="sd">        def baz():</span>
<span class="sd">            foo = 2</span>
<span class="sd">            bar()</span>

<span class="sd">        baz()</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">frame</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">currentframe</span><span class="p">()</span>
    <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">while</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">frames_up</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">frame</span> <span class="o">=</span> <span class="n">frame</span><span class="o">.</span><span class="n">f_back</span>
        <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="n">f_locals</span> <span class="o">=</span> <span class="n">frame</span><span class="o">.</span><span class="n">f_locals</span>
    <span class="n">f_globals</span> <span class="o">=</span> <span class="n">frame</span><span class="o">.</span><span class="n">f_globals</span>

    <span class="k">class</span> <span class="nc">env</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
        <span class="k">def</span> <span class="fm">__getattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">f_locals</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">f_locals</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
            <span class="k">elif</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">f_globals</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">f_globals</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>

    <span class="k">return</span> <span class="n">createResolutionCallbackFromEnv</span><span class="p">(</span><span class="n">env</span><span class="p">())</span>


<span class="k">def</span> <span class="nf">get_closure</span><span class="p">(</span><span class="n">fn</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Get a dictionary of closed over variables from a function</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">captures</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">captures</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">fn</span><span class="o">.</span><span class="vm">__globals__</span><span class="p">)</span>

    <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">captured_name</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">fn</span><span class="o">.</span><span class="vm">__code__</span><span class="o">.</span><span class="n">co_freevars</span><span class="p">):</span>
        <span class="n">captures</span><span class="p">[</span><span class="n">captured_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">fn</span><span class="o">.</span><span class="vm">__closure__</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="o">.</span><span class="n">cell_contents</span>

    <span class="k">return</span> <span class="n">captures</span>

<span class="c1"># [local resolution in python]</span>
<span class="c1"># Depending on where a variable is defined, and where it is used, we may</span>
<span class="c1"># or may not be able to recover its value when recursively compiling a</span>
<span class="c1"># script function. Remember in the general case, a module or function is</span>
<span class="c1"># first defined and then later scripted. This means we do not have a</span>
<span class="c1"># chance to capture the active frames when the function is defined. Hence any</span>
<span class="c1"># name resolution has to happen later on the created closure. The way</span>
<span class="c1"># python captures type annotations restricts what we can recover. The</span>
<span class="c1"># follow example illustrates the different cases:</span>
<span class="c1">#</span>
<span class="c1">#         class MyGlobalClass:</span>
<span class="c1">#         ...</span>
<span class="c1">#         def my_local_scope():</span>
<span class="c1">#             @torch.jit.script</span>
<span class="c1">#             class MyClass:</span>
<span class="c1">#                 ...</span>
<span class="c1">#             @torch.jit.script</span>
<span class="c1">#             class MyClassUsedAsVar:</span>
<span class="c1">#                 ...</span>
<span class="c1">#             def eg(x: MyClass, y: MyGlobalClass):</span>
<span class="c1">#                 a_local_capture : Foo</span>
<span class="c1">#                 return MyClassUsedAsVar(x)</span>
<span class="c1">#</span>
<span class="c1"># MyGlobalClass is defined in the __globals__ dictionary of function</span>
<span class="c1"># &#39;eg&#39;, so it is always recoverable. my_local_scope introduces a new local</span>
<span class="c1"># variable scope in the function. Classes defined here are only visible as</span>
<span class="c1"># local variables. For the case of MyClassUsedAsVar, it is captured</span>
<span class="c1"># because it is used as a variable inside the body of the function, and we</span>
<span class="c1"># can resolve it using the captures returned from `get_closure`. However,</span>
<span class="c1"># the type annotations are not captured by the closure. In Python</span>
<span class="c1"># 3.0--3.9, the _value_ of MyClass and MyGlobalClass will be available as</span>
<span class="c1"># annotations on `eg``, but starting in Python 4.0, they will represented as</span>
<span class="c1"># strings and no longer present. Furthermore, since the body of `eg` does</span>
<span class="c1"># not reference those names, they do not appear in the list of closed over</span>
<span class="c1"># variables. In Python 2.x, type annotations are in comments, leading to a</span>
<span class="c1"># similar situation where their definitions are not available. We anticipate</span>
<span class="c1"># that most users will not run into this issue because their modules and</span>
<span class="c1"># functions will be defined at a global scope like MyGlobalClass. In cases</span>
<span class="c1"># where they are not, it is possible to work around issues by declaring the</span>
<span class="c1"># values global in the function.</span>



<span class="k">def</span> <span class="nf">createResolutionCallbackFromClosure</span><span class="p">(</span><span class="n">fn</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create a resolutionCallback by introspecting the function instead of</span>
<span class="sd">    looking up the stack for the enclosing scope</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">closure</span> <span class="o">=</span> <span class="n">get_closure</span><span class="p">(</span><span class="n">fn</span><span class="p">)</span>

    <span class="k">class</span> <span class="nc">closure_lookup</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
        <span class="c1"># This is a class since `closure` is a dict and it&#39;s easier in</span>
        <span class="c1"># `env_helper` if everything just works with `getattr` calls</span>
        <span class="k">def</span> <span class="fm">__getattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">closure</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">closure</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
            <span class="k">elif</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">builtins</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
                <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">builtins</span><span class="p">,</span> <span class="n">key</span><span class="p">)</span>
            <span class="k">return</span> <span class="kc">None</span>

    <span class="k">return</span> <span class="n">createResolutionCallbackFromEnv</span><span class="p">(</span><span class="n">closure_lookup</span><span class="p">())</span>


<span class="k">def</span> <span class="nf">can_compile_class</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
    <span class="c1"># If any of the functions on a type don&#39;t have a code object, this type can&#39;t</span>
    <span class="c1"># be compiled and is probably a builtin / bound from C</span>
    <span class="k">if</span> <span class="n">is_ignored_fn</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="kc">False</span>
    <span class="n">names</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__dict__</span>
    <span class="n">fns</span> <span class="o">=</span> <span class="p">[</span><span class="nb">getattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">names</span> <span class="k">if</span> <span class="n">inspect</span><span class="o">.</span><span class="n">isroutine</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="kc">None</span><span class="p">))]</span>
    <span class="n">has_code</span> <span class="o">=</span> <span class="p">[</span><span class="nb">hasattr</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="s1">&#39;__code__&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">fn</span> <span class="ow">in</span> <span class="n">fns</span><span class="p">]</span>
    <span class="k">return</span> <span class="nb">all</span><span class="p">(</span><span class="n">has_code</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">createResolutionCallbackForClassMethods</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This looks at all the methods defined in a class and pulls their closed-over</span>
<span class="sd">    variables into a dictionary and uses that to resolve variables.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># cls is a type here, so `ismethod` is false since the methods on the type</span>
    <span class="c1"># aren&#39;t bound to anything, so Python treats them as regular functions</span>
    <span class="n">fns</span> <span class="o">=</span> <span class="p">[</span><span class="nb">getattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__dict__</span> <span class="k">if</span> <span class="n">inspect</span><span class="o">.</span><span class="n">isroutine</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">))]</span>
    <span class="n">captures</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="k">for</span> <span class="n">fn</span> <span class="ow">in</span> <span class="n">fns</span><span class="p">:</span>
        <span class="n">captures</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">get_closure</span><span class="p">(</span><span class="n">fn</span><span class="p">))</span>

    <span class="k">return</span> <span class="k">lambda</span> <span class="n">key</span><span class="p">:</span> <span class="n">captures</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">boolean_dispatch</span><span class="p">(</span><span class="n">arg_name</span><span class="p">,</span> <span class="n">arg_index</span><span class="p">,</span> <span class="n">default</span><span class="p">,</span> <span class="n">if_true</span><span class="p">,</span> <span class="n">if_false</span><span class="p">,</span> <span class="n">module_name</span><span class="p">,</span> <span class="n">func_name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Dispatches to either of 2 script functions based on a boolean argument.</span>
<span class="sd">    In TorchScript, the boolean argument must be constant so that the correct</span>
<span class="sd">    function to use can be determined at compile time.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">fn</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">dispatch_flag</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="n">arg_name</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="n">dispatch_flag</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="n">arg_name</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">arg_index</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">):</span>
            <span class="n">dispatch_flag</span> <span class="o">=</span> <span class="n">args</span><span class="p">[</span><span class="n">arg_index</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">dispatch_flag</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">if_true</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">if_false</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">if_true</span><span class="o">.</span><span class="vm">__doc__</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">if_false</span><span class="o">.</span><span class="vm">__doc__</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">doc</span> <span class="o">=</span> <span class="n">if_false</span><span class="o">.</span><span class="vm">__doc__</span>
        <span class="n">if_true</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="n">doc</span>
    <span class="k">elif</span> <span class="n">if_false</span><span class="o">.</span><span class="vm">__doc__</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">if_true</span><span class="o">.</span><span class="vm">__doc__</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">doc</span> <span class="o">=</span> <span class="n">if_true</span><span class="o">.</span><span class="vm">__doc__</span>
        <span class="n">if_false</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="n">doc</span>
    <span class="k">elif</span> <span class="n">if_false</span><span class="o">.</span><span class="vm">__doc__</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">if_true</span><span class="o">.</span><span class="vm">__doc__</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="c1"># neither function has a docstring</span>
        <span class="n">doc</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;only one function can have a docstring&quot;</span><span class="p">)</span>
    <span class="n">fn</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="n">doc</span>

    <span class="k">if</span> <span class="n">module_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">fn</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">=</span> <span class="n">module_name</span>
    <span class="k">if</span> <span class="n">func_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">fn</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">=</span> <span class="n">func_name</span>

    <span class="n">boolean_dispatched</span><span class="p">[</span><span class="n">fn</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s2">&quot;if_true&quot;</span><span class="p">:</span> <span class="n">if_true</span><span class="p">,</span>
        <span class="s2">&quot;if_false&quot;</span><span class="p">:</span> <span class="n">if_false</span><span class="p">,</span>
        <span class="s2">&quot;index&quot;</span><span class="p">:</span> <span class="n">arg_index</span><span class="p">,</span>
        <span class="s2">&quot;default&quot;</span><span class="p">:</span> <span class="n">default</span><span class="p">,</span>
        <span class="s2">&quot;arg_name&quot;</span><span class="p">:</span> <span class="n">arg_name</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="n">fn</span>


<span class="k">class</span> <span class="nc">FunctionModifiers</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Used to denote the behavior of a function in TorchScript. See export() and</span>
<span class="sd">    ignore() for details.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">UNUSED</span> <span class="o">=</span> <span class="s2">&quot;unused (ignored and replaced with raising of an exception)&quot;</span>
    <span class="n">IGNORE</span> <span class="o">=</span> <span class="s2">&quot;ignore (leave as a call to Python, cannot be torch.jit.save&#39;d)&quot;</span>
    <span class="n">EXPORT</span> <span class="o">=</span> <span class="s2">&quot;export (compile this function even if nothing calls it)&quot;</span>
    <span class="n">DEFAULT</span> <span class="o">=</span> <span class="s2">&quot;default (compile if called from a exported function / forward)&quot;</span>
    <span class="n">COPY_TO_SCRIPT_WRAPPER</span> <span class="o">=</span> \
        <span class="s2">&quot;if this method is not scripted, copy the python method onto the scripted model&quot;</span>


<div class="viewcode-block" id="export"><a class="viewcode-back" href="../../jit.html#torch.jit.export">[docs]</a><span class="k">def</span> <span class="nf">export</span><span class="p">(</span><span class="n">fn</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This decorator indicates that a method on an ``nn.Module`` is used as an entry point into a</span>
<span class="sd">    :class:`ScriptModule` and should be compiled.</span>

<span class="sd">    ``forward`` implicitly is assumed to be an entry point, so it does not need this decorator.</span>
<span class="sd">    Functions and methods called from ``forward`` are compiled as they are seen</span>
<span class="sd">    by the compiler, so they do not need this decorator either.</span>

<span class="sd">    Example (using ``@torch.jit.export`` on a method):</span>

<span class="sd">    .. testcode::</span>

<span class="sd">        import torch</span>
<span class="sd">        import torch.nn as nn</span>

<span class="sd">        class MyModule(nn.Module):</span>
<span class="sd">            def implicitly_compiled_method(self, x):</span>
<span class="sd">                return x + 99</span>

<span class="sd">            # `forward` is implicitly decorated with `@torch.jit.export`,</span>
<span class="sd">            # so adding it here would have no effect</span>
<span class="sd">            def forward(self, x):</span>
<span class="sd">                return x + 10</span>

<span class="sd">            @torch.jit.export</span>
<span class="sd">            def another_forward(self, x):</span>
<span class="sd">                # When the compiler sees this call, it will compile</span>
<span class="sd">                # `implicitly_compiled_method`</span>
<span class="sd">                return self.implicitly_compiled_method(x)</span>

<span class="sd">            def unused_method(self, x):</span>
<span class="sd">                return x - 20</span>

<span class="sd">        # `m` will contain compiled methods:</span>
<span class="sd">        #     `forward`</span>
<span class="sd">        #     `another_forward`</span>
<span class="sd">        #     `implicitly_compiled_method`</span>
<span class="sd">        # `unused_method` will not be compiled since it was not called from</span>
<span class="sd">        # any compiled methods and wasn&#39;t decorated with `@torch.jit.export`</span>
<span class="sd">        m = torch.jit.script(MyModule())</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">fn</span><span class="o">.</span><span class="n">_torchscript_modifier</span> <span class="o">=</span> <span class="n">FunctionModifiers</span><span class="o">.</span><span class="n">EXPORT</span>
    <span class="k">return</span> <span class="n">fn</span></div>


<div class="viewcode-block" id="unused"><a class="viewcode-back" href="../../jit.html#torch.jit.unused">[docs]</a><span class="k">def</span> <span class="nf">unused</span><span class="p">(</span><span class="n">fn</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This decorator indicates to the compiler that a function or method should</span>
<span class="sd">    be ignored and replaced with the raising of an exception. This allows you</span>
<span class="sd">    to leave code in your model that is not yet TorchScript compatible and still</span>
<span class="sd">    export your model.</span>

<span class="sd">        Example (using ``@torch.jit.unused`` on a method)::</span>

<span class="sd">            import torch</span>
<span class="sd">            import torch.nn as nn</span>

<span class="sd">            class MyModule(nn.Module):</span>
<span class="sd">                def __init__(self, use_memory_efficent):</span>
<span class="sd">                    super(MyModule, self).__init__()</span>
<span class="sd">                    self.use_memory_efficent = use_memory_efficent</span>

<span class="sd">                @torch.jit.unused</span>
<span class="sd">                def memory_efficient(self, x):</span>
<span class="sd">                    import pdb</span>
<span class="sd">                    pdb.set_trace()</span>
<span class="sd">                    return x + 10</span>

<span class="sd">                def forward(self, x):</span>
<span class="sd">                    # Use not-yet-scriptable memory efficient mode</span>
<span class="sd">                    if self.use_memory_efficient:</span>
<span class="sd">                        return self.memory_efficient(x)</span>
<span class="sd">                    else:</span>
<span class="sd">                        return x + 10</span>

<span class="sd">            m = torch.jit.script(MyModule(use_memory_efficent=False))</span>
<span class="sd">            m.save(&quot;m.pt&quot;)</span>

<span class="sd">            m = torch.jit.script(MyModule(use_memory_efficient=True))</span>
<span class="sd">            # exception raised</span>
<span class="sd">            m(torch.rand(100))</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">fn</span><span class="o">.</span><span class="n">_torchscript_modifier</span> <span class="o">=</span> <span class="n">FunctionModifiers</span><span class="o">.</span><span class="n">UNUSED</span>
    <span class="k">return</span> <span class="n">fn</span></div>

<div class="viewcode-block" id="ignore"><a class="viewcode-back" href="../../jit.html#torch.jit.ignore">[docs]</a><span class="k">def</span> <span class="nf">ignore</span><span class="p">(</span><span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This decorator indicates to the compiler that a function or method should</span>
<span class="sd">    be ignored and left as a Python function. This allows you to leave code in</span>
<span class="sd">    your model that is not yet TorchScript compatible. If called from TorchScript,</span>
<span class="sd">    ignored functions will dispatch the call to the Python interpreter. Models with ignored</span>
<span class="sd">    functions cannot be exported; use :func:`@torch.jit.unused &lt;torch.jit.unused&gt;` instead.</span>

<span class="sd">    Example (using ``@torch.jit.ignore`` on a method)::</span>

<span class="sd">        import torch</span>
<span class="sd">        import torch.nn as nn</span>

<span class="sd">        class MyModule(nn.Module):</span>
<span class="sd">            @torch.jit.ignore</span>
<span class="sd">            def debugger(self, x):</span>
<span class="sd">                import pdb</span>
<span class="sd">                pdb.set_trace()</span>

<span class="sd">            def forward(self, x):</span>
<span class="sd">                x += 10</span>
<span class="sd">                # The compiler would normally try to compile `debugger`,</span>
<span class="sd">                # but since it is `@ignore`d, it will be left as a call</span>
<span class="sd">                # to Python</span>
<span class="sd">                self.debugger(x)</span>
<span class="sd">                return x</span>

<span class="sd">        m = torch.jit.script(MyModule())</span>

<span class="sd">        # Error! The call `debugger` cannot be saved since it calls into Python</span>
<span class="sd">        m.save(&quot;m.pt&quot;)</span>

<span class="sd">    Example (using ``@torch.jit.ignore(drop=True)`` on a method):</span>

<span class="sd">    .. testcode::</span>

<span class="sd">        import torch</span>
<span class="sd">        import torch.nn as nn</span>

<span class="sd">        class MyModule(nn.Module):</span>
<span class="sd">            @torch.jit.ignore(drop=True)</span>
<span class="sd">            def training_method(self, x):</span>
<span class="sd">                import pdb</span>
<span class="sd">                pdb.set_trace()</span>

<span class="sd">            def forward(self, x):</span>
<span class="sd">                if self.training:</span>
<span class="sd">                    self.training_method(x)</span>
<span class="sd">                return x</span>

<span class="sd">        m = torch.jit.script(MyModule())</span>

<span class="sd">        # This is OK since `training_method` is not saved, the call is replaced</span>
<span class="sd">        # with a `raise`.</span>
<span class="sd">        m.save(&quot;m.pt&quot;)</span>

<span class="sd">    .. testcleanup::</span>

<span class="sd">        import os</span>
<span class="sd">        os.remove(&#39;m.pt&#39;)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="n">drop</span><span class="p">):</span>
        <span class="c1"># used without any args, so drop is actually a function</span>
        <span class="c1">#   @torch.jit.ignore</span>
        <span class="c1">#   def fn(...):</span>
        <span class="n">fn</span> <span class="o">=</span> <span class="n">drop</span>
        <span class="n">fn</span><span class="o">.</span><span class="n">_torchscript_modifier</span> <span class="o">=</span> <span class="n">FunctionModifiers</span><span class="o">.</span><span class="n">IGNORE</span>
        <span class="k">return</span> <span class="n">fn</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">drop</span><span class="p">,</span> <span class="nb">bool</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Argument to @torch.jit.ignore must be a bool or &quot;</span>
                           <span class="s2">&quot;a function but got </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">drop</span><span class="p">))</span>

    <span class="c1"># for backwards compat</span>
    <span class="n">drop_on_export</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s2">&quot;drop_on_export&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">drop_on_export</span><span class="p">:</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;ignore(drop_on_export=True) has been deprecated. TorchScript will now drop the function &quot;</span>
                      <span class="s2">&quot;call on compilation. Use torch.jit.unused now. </span><span class="si">{}</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">category</span><span class="o">=</span><span class="ne">FutureWarning</span><span class="p">)</span>

        <span class="n">drop</span> <span class="o">=</span> <span class="n">drop_on_export</span>
    <span class="k">elif</span> <span class="n">drop</span><span class="p">:</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;ignore(True) has been deprecated. TorchScript will now drop the function &quot;</span>
                      <span class="s2">&quot;call on compilation. Use torch.jit.unused now. </span><span class="si">{}</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">category</span><span class="o">=</span><span class="ne">FutureWarning</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">decorator</span><span class="p">(</span><span class="n">fn</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">drop</span><span class="p">:</span>
            <span class="n">fn</span><span class="o">.</span><span class="n">_torchscript_modifier</span> <span class="o">=</span> <span class="n">FunctionModifiers</span><span class="o">.</span><span class="n">UNUSED</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">fn</span><span class="o">.</span><span class="n">_torchscript_modifier</span> <span class="o">=</span> <span class="n">FunctionModifiers</span><span class="o">.</span><span class="n">IGNORE</span>
        <span class="k">return</span> <span class="n">fn</span>
    <span class="k">return</span> <span class="n">decorator</span></div>


<span class="k">def</span> <span class="nf">_copy_to_script_wrapper</span><span class="p">(</span><span class="n">fn</span><span class="p">):</span>
    <span class="n">fn</span><span class="o">.</span><span class="n">_torchscript_modifier</span> <span class="o">=</span> <span class="n">FunctionModifiers</span><span class="o">.</span><span class="n">COPY_TO_SCRIPT_WRAPPER</span>
    <span class="k">return</span> <span class="n">fn</span>

<span class="k">def</span> <span class="nf">module_has_exports</span><span class="p">(</span><span class="n">mod</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">(</span><span class="n">mod</span><span class="p">):</span>
        <span class="n">item</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="n">item</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">get_torchscript_modifier</span><span class="p">(</span><span class="n">item</span><span class="p">)</span> <span class="ow">is</span> <span class="n">FunctionModifiers</span><span class="o">.</span><span class="n">EXPORT</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">True</span>
    <span class="k">return</span> <span class="kc">False</span>

<span class="k">def</span> <span class="nf">should_drop</span><span class="p">(</span><span class="n">fn</span><span class="p">):</span>
    <span class="n">attr</span> <span class="o">=</span> <span class="n">get_torchscript_modifier</span><span class="p">(</span><span class="n">fn</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">attr</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">False</span>
    <span class="k">return</span> <span class="n">attr</span> <span class="ow">is</span> <span class="n">FunctionModifiers</span><span class="o">.</span><span class="n">UNUSED</span>


<span class="k">def</span> <span class="nf">is_ignored_fn</span><span class="p">(</span><span class="n">fn</span><span class="p">):</span>
    <span class="n">mod</span> <span class="o">=</span> <span class="n">get_torchscript_modifier</span><span class="p">(</span><span class="n">fn</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">mod</span> <span class="ow">is</span> <span class="n">FunctionModifiers</span><span class="o">.</span><span class="n">UNUSED</span> <span class="ow">or</span> <span class="n">mod</span> <span class="ow">is</span> <span class="n">FunctionModifiers</span><span class="o">.</span><span class="n">IGNORE</span>

<span class="k">def</span> <span class="nf">get_torchscript_modifier</span><span class="p">(</span><span class="n">fn</span><span class="p">):</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">callable</span><span class="p">(</span><span class="n">fn</span><span class="p">):</span>
        <span class="k">return</span> <span class="kc">None</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="s1">&#39;__func__&#39;</span><span class="p">):</span>
        <span class="n">fn</span> <span class="o">=</span> <span class="n">fn</span><span class="o">.</span><span class="vm">__func__</span>
    <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="s1">&#39;_torchscript_modifier&#39;</span><span class="p">,</span> <span class="n">FunctionModifiers</span><span class="o">.</span><span class="n">DEFAULT</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">copy_torchscript_modifier</span><span class="p">(</span><span class="n">orig</span><span class="p">,</span> <span class="n">new</span><span class="p">):</span>
    <span class="n">attr</span> <span class="o">=</span> <span class="n">get_torchscript_modifier</span><span class="p">(</span><span class="n">orig</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">attr</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span>
    <span class="n">new</span><span class="o">.</span><span class="n">_torchscript_modifier</span> <span class="o">=</span> <span class="n">attr</span>

<span class="c1"># overloading registration</span>
<span class="c1"># overloads get registered in this file, and compiled in torch/jit/__init__.py</span>
<span class="c1"># so that they can be imported in nn/functional.py without an import cycle</span>

<span class="c1"># qualified_name =&gt; list[overload_functions]</span>
<span class="n">_overloaded_fns</span> <span class="o">=</span> <span class="p">{}</span>  <span class="c1"># noqa: T484</span>

<span class="k">def</span> <span class="nf">_overload</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
    <span class="n">qual_name</span> <span class="o">=</span> <span class="n">_qualified_name</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
    <span class="k">global</span> <span class="n">_overloaded_fns</span>
    <span class="n">fn_overload_list</span> <span class="o">=</span> <span class="n">_overloaded_fns</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">qual_name</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">fn_overload_list</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">fn_overload_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">_overloaded_fns</span><span class="p">[</span><span class="n">qual_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">fn_overload_list</span>
    <span class="n">fn_overload_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">func</span>

<span class="k">def</span> <span class="nf">_get_fn_overloads</span><span class="p">(</span><span class="n">qual_name</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">_overloaded_fns</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">qual_name</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">_clear_fn_overloads</span><span class="p">(</span><span class="n">qual_name</span><span class="p">):</span>
    <span class="k">del</span> <span class="n">_overloaded_fns</span><span class="p">[</span><span class="n">qual_name</span><span class="p">]</span>

<span class="k">def</span> <span class="nf">get_class_name_lineno</span><span class="p">(</span><span class="n">method</span><span class="p">):</span>
    <span class="n">current_frame</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">currentframe</span><span class="p">()</span>

    <span class="c1"># one for the get_class_name call, one for _overload_method call</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">):</span>
        <span class="n">current_frame</span> <span class="o">=</span> <span class="n">current_frame</span><span class="o">.</span><span class="n">f_back</span>
    <span class="n">class_name</span> <span class="o">=</span> <span class="n">current_frame</span><span class="o">.</span><span class="n">f_code</span><span class="o">.</span><span class="n">co_name</span>
    <span class="n">line_no</span> <span class="o">=</span> <span class="n">current_frame</span><span class="o">.</span><span class="n">f_code</span><span class="o">.</span><span class="n">co_firstlineno</span>
    <span class="k">return</span> <span class="n">class_name</span><span class="p">,</span> <span class="n">line_no</span>

<span class="c1"># At the the point the decorator is applied to class methods the method</span>
<span class="c1"># has no reference to its owning class. _qualified_name would not include</span>
<span class="c1"># the class it is defined in, so any methods with the same name in the same file</span>
<span class="c1"># would have the same _qualified_name, even if they were defined in different</span>
<span class="c1"># classes. This problem only exists in python 2.</span>
<span class="c1"># We get around this problem by looking at the stack frame and identifying</span>
<span class="c1"># the class name, and throwing an error whenever overloads are used</span>
<span class="c1"># when modules of the same name are in the same file</span>

<span class="c1"># qualified_name =&gt; class name =&gt; list[overload_functions]</span>
<span class="n">_overloaded_methods</span> <span class="o">=</span> <span class="p">{}</span>  <span class="c1"># noqa: T484</span>


<span class="c1"># (qualified_name, class name) =&gt; class_fileno</span>
<span class="n">_overloaded_method_class_fileno</span> <span class="o">=</span> <span class="p">{}</span>

<span class="k">def</span> <span class="nf">_overload_method</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
    <span class="n">qual_name</span> <span class="o">=</span> <span class="n">_qualified_name</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
    <span class="k">global</span> <span class="n">_overloaded_methods</span>
    <span class="n">class_name_map</span> <span class="o">=</span> <span class="n">_overloaded_methods</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">qual_name</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">class_name_map</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">class_name_map</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">_overloaded_methods</span><span class="p">[</span><span class="n">qual_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">class_name_map</span>

    <span class="n">class_name</span><span class="p">,</span> <span class="n">line_no</span> <span class="o">=</span> <span class="n">get_class_name_lineno</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
    <span class="n">method_overloads</span> <span class="o">=</span> <span class="n">class_name_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">class_name</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">method_overloads</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">method_overloads</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">class_name_map</span><span class="p">[</span><span class="n">class_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">method_overloads</span>
        <span class="n">_overloaded_method_class_fileno</span><span class="p">[(</span><span class="n">qual_name</span><span class="p">,</span> <span class="n">class_name</span><span class="p">)]</span> <span class="o">=</span> <span class="n">line_no</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">existing_lineno</span> <span class="o">=</span> <span class="n">_overloaded_method_class_fileno</span><span class="p">[(</span><span class="n">qual_name</span><span class="p">,</span> <span class="n">class_name</span><span class="p">)]</span>
        <span class="k">if</span> <span class="n">existing_lineno</span> <span class="o">!=</span> <span class="n">line_no</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Cannot currently overload the same method name in two different&quot;</span>
                               <span class="s2">&quot; classes with the same name in the same module&quot;</span><span class="p">)</span>

    <span class="n">method_overloads</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">func</span>

<span class="k">def</span> <span class="nf">_get_overloaded_methods</span><span class="p">(</span><span class="n">method</span><span class="p">,</span> <span class="n">mod_class</span><span class="p">):</span>
    <span class="c1"># TODO: __name__ not set for submodules in recursive script</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">method</span><span class="p">,</span> <span class="s2">&quot;__name__&quot;</span><span class="p">):</span>
        <span class="k">return</span> <span class="kc">None</span>
    <span class="n">qual_name</span> <span class="o">=</span> <span class="n">_qualified_name</span><span class="p">(</span><span class="n">method</span><span class="p">)</span>
    <span class="n">class_name_map</span> <span class="o">=</span> <span class="n">_overloaded_methods</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">qual_name</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">class_name_map</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">None</span>
    <span class="n">overloads</span> <span class="o">=</span> <span class="n">class_name_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">mod_class</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">overloads</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">None</span>

    <span class="n">method_line_no</span> <span class="o">=</span> <span class="n">get_source_lines_and_file</span><span class="p">(</span><span class="n">method</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
    <span class="n">mod_class_fileno</span> <span class="o">=</span> <span class="n">get_source_lines_and_file</span><span class="p">(</span><span class="n">mod_class</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
    <span class="n">mod_end_fileno</span> <span class="o">=</span> <span class="n">mod_class_fileno</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">get_source_lines_and_file</span><span class="p">(</span><span class="n">mod_class</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">method_line_no</span> <span class="o">&gt;=</span> <span class="n">mod_class_fileno</span> <span class="ow">and</span> <span class="n">method_line_no</span> <span class="o">&lt;=</span> <span class="n">mod_end_fileno</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">&quot;Overloads are not useable when a module is redeclared within the same file: &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">method</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">overloads</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">typing</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Tuple</span><span class="p">,</span> <span class="n">List</span><span class="p">,</span> <span class="n">Dict</span><span class="p">,</span> <span class="n">Optional</span><span class="p">,</span> <span class="n">Any</span>

    <span class="k">def</span> <span class="nf">is_tuple</span><span class="p">(</span><span class="n">ann</span><span class="p">):</span>
        <span class="c1"># For some reason Python 3.7 violates the Type[A, B].__origin__ == Type rule</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="s1">&#39;__module__&#39;</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="n">ann</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">==</span> <span class="s1">&#39;typing&#39;</span> <span class="ow">and</span> \
            <span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="s1">&#39;__origin__&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="n">typing</span><span class="o">.</span><span class="n">Tuple</span> <span class="ow">or</span>
             <span class="nb">getattr</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="s1">&#39;__origin__&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">tuple</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">is_list</span><span class="p">(</span><span class="n">ann</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="s1">&#39;__module__&#39;</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="n">ann</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">==</span> <span class="s1">&#39;typing&#39;</span> <span class="ow">and</span> \
            <span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="s1">&#39;__origin__&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="n">typing</span><span class="o">.</span><span class="n">List</span> <span class="ow">or</span>
             <span class="nb">getattr</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="s1">&#39;__origin__&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">list</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">is_dict</span><span class="p">(</span><span class="n">ann</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="s1">&#39;__module__&#39;</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="n">ann</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">==</span> <span class="s1">&#39;typing&#39;</span> <span class="ow">and</span> \
            <span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="s1">&#39;__origin__&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="n">typing</span><span class="o">.</span><span class="n">Dict</span> <span class="ow">or</span>
             <span class="nb">getattr</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="s1">&#39;__origin__&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">dict</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">is_optional</span><span class="p">(</span><span class="n">ann</span><span class="p">):</span>
        <span class="c1"># Optional[T] is just shorthand for Union[T, None], so check for both</span>
        <span class="k">def</span> <span class="nf">safe_is_subclass</span><span class="p">(</span><span class="n">the_type</span><span class="p">,</span> <span class="n">super_type</span><span class="p">):</span>
            <span class="c1"># Don&#39;t throw if `the_type` isn&#39;t a class type (e.g. if it is</span>
            <span class="c1"># another type annotation instance)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">inspect</span><span class="o">.</span><span class="n">isclass</span><span class="p">(</span><span class="n">the_type</span><span class="p">):</span>
                <span class="k">return</span> <span class="kc">False</span>
            <span class="k">return</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">the_type</span><span class="p">,</span> <span class="n">super_type</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="s1">&#39;__module__&#39;</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="n">union_optional</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="n">ann</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">==</span> <span class="s1">&#39;typing&#39;</span> <span class="ow">and</span> \
           <span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="s1">&#39;__origin__&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">):</span>
            <span class="n">args</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="s1">&#39;__args__&#39;</span><span class="p">,</span> <span class="p">())</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                <span class="n">union_optional</span> <span class="o">=</span> <span class="p">(</span><span class="n">safe_is_subclass</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="nb">type</span><span class="p">(</span><span class="kc">None</span><span class="p">))</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">safe_is_subclass</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">type</span><span class="p">(</span><span class="kc">None</span><span class="p">)))</span> \
                    <span class="ow">or</span> <span class="p">(</span><span class="n">safe_is_subclass</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">type</span><span class="p">(</span><span class="kc">None</span><span class="p">))</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">safe_is_subclass</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="nb">type</span><span class="p">(</span><span class="kc">None</span><span class="p">)))</span>

        <span class="n">optional</span> <span class="o">=</span> <span class="n">ann</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">==</span> <span class="s1">&#39;typing&#39;</span> <span class="ow">and</span> \
            <span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="s1">&#39;__origin__&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="n">typing</span><span class="o">.</span><span class="n">Optional</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">optional</span> <span class="ow">or</span> <span class="n">union_optional</span>

<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="c1"># A minimal polyfill for versions of Python that don&#39;t have typing.</span>
    <span class="c1"># Note that this means that they also don&#39;t support the fancy annotation syntax, so</span>
    <span class="c1"># those instances will only be used in our tiny `type: ` comment interpreter.</span>

    <span class="c1"># The __getitem__ in typing is implemented using metaclasses, but I&#39;m too lazy for that.</span>
    <span class="k">class</span> <span class="nc">TupleCls</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
        <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">types</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">TupleInstance</span><span class="p">(</span><span class="n">types</span><span class="p">)</span>

    <span class="k">class</span> <span class="nc">TupleInstance</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;__args__&#39;</span><span class="p">]</span>

        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">types</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span> <span class="o">=</span> <span class="n">types</span>

    <span class="k">class</span> <span class="nc">ListInstance</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;__args__&#39;</span><span class="p">]</span>

        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">types</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span> <span class="o">=</span> <span class="n">types</span>

    <span class="k">class</span> <span class="nc">ListCls</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
        <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">types</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">TupleInstance</span><span class="p">(</span><span class="n">types</span><span class="p">)</span>

    <span class="k">class</span> <span class="nc">DictInstance</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;__args__&#39;</span><span class="p">]</span>

        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">types</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span> <span class="o">=</span> <span class="n">types</span>

    <span class="k">class</span> <span class="nc">DictCls</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
        <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">types</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">DictInstance</span><span class="p">(</span><span class="n">types</span><span class="p">)</span>

    <span class="k">class</span> <span class="nc">OptionalInstance</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;__args__&#39;</span><span class="p">]</span>

        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">types</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span> <span class="o">=</span> <span class="n">types</span>

    <span class="k">class</span> <span class="nc">OptionalCls</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
        <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">types</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">OptionalInstance</span><span class="p">(</span><span class="n">types</span><span class="p">)</span>

    <span class="k">class</span> <span class="nc">AnyCls</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="n">Tuple</span> <span class="o">=</span> <span class="n">TupleCls</span><span class="p">()</span>  <span class="c1"># noqa: T484</span>
    <span class="n">List</span> <span class="o">=</span> <span class="n">ListCls</span><span class="p">()</span>  <span class="c1"># noqa: T484</span>
    <span class="n">Dict</span> <span class="o">=</span> <span class="n">DictCls</span><span class="p">()</span>  <span class="c1"># noqa: T484</span>
    <span class="n">Optional</span> <span class="o">=</span> <span class="n">DictCls</span><span class="p">()</span>  <span class="c1"># noqa: T484</span>
    <span class="n">Any</span> <span class="o">=</span> <span class="n">AnyCls</span><span class="p">()</span>  <span class="c1"># noqa: T484</span>

    <span class="k">def</span> <span class="nf">is_tuple</span><span class="p">(</span><span class="n">ann</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="n">TupleInstance</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">is_list</span><span class="p">(</span><span class="n">ann</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="n">ListInstance</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">is_dict</span><span class="p">(</span><span class="n">ann</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="n">DictInstance</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">is_optional</span><span class="p">(</span><span class="n">ann</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="n">OptionalInstance</span><span class="p">)</span>


<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">typing_extensions</span>
    <span class="kn">from</span> <span class="nn">typing_extensions</span> <span class="kn">import</span> <span class="n">Final</span>

    <span class="k">def</span> <span class="nf">is_final</span><span class="p">(</span><span class="n">ann</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">ann</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">==</span> <span class="s1">&#39;typing_extensions&#39;</span> <span class="ow">and</span> \
            <span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="s1">&#39;__origin__&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="n">typing_extensions</span><span class="o">.</span><span class="n">Final</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="c1"># Same as above, this polyfill is only for `typing_extensions`</span>
    <span class="k">class</span> <span class="nc">FinalInstance</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;__args__&#39;</span><span class="p">]</span>

        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">types</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span> <span class="o">=</span> <span class="n">types</span>

    <span class="k">class</span> <span class="nc">FinalCls</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
        <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">types</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">FinalInstance</span><span class="p">(</span><span class="n">types</span><span class="p">)</span>

    <span class="n">Final</span> <span class="o">=</span> <span class="n">FinalCls</span><span class="p">()</span>  <span class="c1"># noqa: T484</span>

    <span class="k">def</span> <span class="nf">is_final</span><span class="p">(</span><span class="n">ann</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="n">FinalInstance</span><span class="p">)</span>


<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">TypeVar</span><span class="p">,</span> <span class="n">Generic</span>

    <span class="n">T</span> <span class="o">=</span> <span class="n">TypeVar</span><span class="p">(</span><span class="s1">&#39;T&#39;</span><span class="p">)</span>

    <span class="k">class</span> <span class="nc">RRef</span><span class="p">(</span><span class="n">Generic</span><span class="p">[</span><span class="n">T</span><span class="p">]):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;__args__&#39;</span><span class="p">]</span>

        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">types</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span> <span class="o">=</span> <span class="n">types</span>

    <span class="k">def</span> <span class="nf">is_rref</span><span class="p">(</span><span class="n">ann</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="s2">&quot;__origin__&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="n">RRef</span>

<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="k">class</span> <span class="nc">RRefInstance</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;__args__&#39;</span><span class="p">]</span>

        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">types</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span> <span class="o">=</span> <span class="n">types</span>

    <span class="k">class</span> <span class="nc">RRefCls</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
        <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">types</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">RRefInstance</span><span class="p">(</span><span class="n">types</span><span class="p">)</span>

    <span class="n">RRef</span> <span class="o">=</span> <span class="n">RRefCls</span><span class="p">()</span>  <span class="c1"># noqa: T484</span>

    <span class="k">def</span> <span class="nf">is_rref</span><span class="p">(</span><span class="n">ann</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">ann</span><span class="p">,</span> <span class="n">RRefInstance</span><span class="p">)</span>


<span class="c1"># allows BroadcastingList instance to be subscriptable</span>
<span class="k">class</span> <span class="nc">BroadcastingListCls</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">types</span><span class="p">):</span>
        <span class="k">return</span>

<span class="c1"># mypy doesn&#39;t support parameters on types, so we have to explicitly type each</span>
<span class="c1"># list size</span>
<span class="n">BroadcastingList1</span> <span class="o">=</span> <span class="n">BroadcastingListCls</span><span class="p">()</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">7</span><span class="p">):</span>
    <span class="nb">globals</span><span class="p">()[</span><span class="s2">&quot;BroadcastingList</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="p">)]</span> <span class="o">=</span> <span class="n">BroadcastingList1</span>

<span class="c1"># Retrieves a fully-qualified name (module hierarchy + classname) for a given obj.</span>
<span class="k">def</span> <span class="nf">_qualified_name</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span>
    <span class="c1"># This special case allows us to override the qualified name on a type.</span>
    <span class="c1"># It&#39;s currently used in conjunction with tracing, where we create a</span>
    <span class="c1"># fake module to filter only supported attributes. However, since this</span>
    <span class="c1"># new type is defined as a local class, we need a mechanism to override</span>
    <span class="c1"># its qualname so it appears correctly in the TorchScript system. This,</span>
    <span class="c1"># we set &#39;_jit_override_qualname&#39; with the original traced module&#39;s</span>
    <span class="c1"># qualified name, which is picked up here</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s1">&#39;_jit_override_qualname&#39;</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">_jit_override_qualname</span>
    <span class="c1"># short-circuit in cases where the object already has a known qualified name</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">torch</span><span class="o">.</span><span class="n">_C</span><span class="o">.</span><span class="n">ScriptFunction</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">qualified_name</span>

    <span class="n">name</span> <span class="o">=</span> <span class="n">obj</span><span class="o">.</span><span class="vm">__name__</span>
    <span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;&lt;lambda&gt;&#39;</span><span class="p">:</span>
        <span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;_lambda&#39;</span>  <span class="c1"># make name a valid identifier</span>

    <span class="n">module_name</span> <span class="o">=</span> <span class="n">obj</span><span class="o">.</span><span class="vm">__module__</span>

    <span class="c1"># If the module is actually a torchbind module, then we should short circuit</span>
    <span class="k">if</span> <span class="n">module_name</span> <span class="o">==</span> <span class="s2">&quot;torch._classes&quot;</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">qualified_name</span>

    <span class="c1"># The Python docs are very clear that `__module__` can be None, but I can&#39;t</span>
    <span class="c1"># figure out when it actually would be.</span>
    <span class="k">if</span> <span class="n">module_name</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Could not get qualified name for class &#39;</span><span class="si">{}</span><span class="s2">&#39;: &quot;</span>
                           <span class="s2">&quot;__module__ can&#39;t be None.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">))</span>

    <span class="c1"># if getattr(sys.modules[module_name], name) is not obj:</span>
    <span class="c1">#     raise RuntimeError(&quot;Could not get qualified name for class &#39;{}&#39;: &quot;</span>
    <span class="c1">#                        &quot;the attr {} on module {} is not the the class&quot;.format(name, name, module_name))</span>

    <span class="c1"># __main__ is a builtin module, so rewrite it to &quot;__torch__&quot;.</span>
    <span class="k">if</span> <span class="n">module_name</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
        <span class="n">module_name</span> <span class="o">=</span> <span class="s2">&quot;__torch__&quot;</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># Everything else gets a &quot;__torch__&quot; prefix to avoid name collisions</span>
        <span class="c1"># with the names of user values.</span>
        <span class="n">module_name</span> <span class="o">=</span> <span class="s2">&quot;__torch__.&quot;</span> <span class="o">+</span> <span class="n">module_name</span>

    <span class="k">if</span> <span class="s2">&quot;.&quot;</span> <span class="ow">in</span> <span class="n">name</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Could not get qualified name for class &#39;</span><span class="si">{}</span><span class="s2">&#39;: &quot;</span>
                           <span class="s2">&quot;&#39;</span><span class="si">{}</span><span class="s2">&#39; is not a valid identifier&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>

    <span class="k">return</span> <span class="n">module_name</span> <span class="o">+</span> <span class="s2">&quot;.&quot;</span> <span class="o">+</span> <span class="n">name</span>


<span class="c1"># Thin wrapper around SourceRangeFactory to store extra metadata</span>
<span class="c1"># about the function-to-be-compiled.</span>
<span class="k">class</span> <span class="nc">SourceContext</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">_C</span><span class="o">.</span><span class="n">_jit_tree_views</span><span class="o">.</span><span class="n">SourceRangeFactory</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">source</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">file_lineno</span><span class="p">,</span> <span class="n">leading_whitespace_len</span><span class="p">,</span> <span class="n">uses_true_division</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SourceContext</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">file_lineno</span><span class="p">,</span> <span class="n">leading_whitespace_len</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">uses_true_division</span> <span class="o">=</span> <span class="n">uses_true_division</span>


<span class="k">def</span> <span class="nf">fake_range</span><span class="p">():</span>
    <span class="k">return</span> <span class="n">SourceContext</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">make_raw_range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
</pre></div>

             </article>
             
            </div>
            <footer>
  

  

    <hr>

  

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Torch Contributors.

    </p>
  </div>
    
      <div>
        Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
      </div>
     

</footer>

          </div>
        </div>

        <div class="pytorch-content-right" id="pytorch-content-right">
          <div class="pytorch-right-menu" id="pytorch-right-menu">
            <div class="pytorch-side-scroll" id="pytorch-side-scroll-right">
              
            </div>
          </div>
        </div>
      </section>
    </div>

  


  

     
       <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
         <script src="../../_static/jquery.js"></script>
         <script src="../../_static/underscore.js"></script>
         <script src="../../_static/doctools.js"></script>
         <script src="../../_static/language_data.js"></script>
     

  

  <script type="text/javascript" src="../../_static/js/vendor/popper.min.js"></script>
  <script type="text/javascript" src="../../_static/js/vendor/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>
  <script type="text/javascript" src="../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>
 
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-90545585-1', 'auto');
  ga('send', 'pageview');

</script>

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-117752657-2"></script>

<script>
  window.dataLayer = window.dataLayer || [];

  function gtag(){dataLayer.push(arguments);}

  gtag('js', new Date());
  gtag('config', 'UA-117752657-2');
</script>

<img height="1" width="1" style="border-style:none;" alt="" src="https://www.googleadservices.com/pagead/conversion/795629140/?label=txkmCPmdtosBENSssfsC&amp;guid=ON&amp;script=0"/>


  <!-- Begin Footer -->

  <div class="container-fluid docs-tutorials-resources" id="docs-tutorials-resources">
    <div class="container">
      <div class="row">
        <div class="col-md-4 text-center">
          <h2>Docs</h2>
          <p>Access comprehensive developer documentation for PyTorch</p>
          <a class="with-right-arrow" href="https://pytorch.org/docs/stable/index.html">View Docs</a>
        </div>

        <div class="col-md-4 text-center">
          <h2>Tutorials</h2>
          <p>Get in-depth tutorials for beginners and advanced developers</p>
          <a class="with-right-arrow" href="https://pytorch.org/tutorials">View Tutorials</a>
        </div>

        <div class="col-md-4 text-center">
          <h2>Resources</h2>
          <p>Find development resources and get your questions answered</p>
          <a class="with-right-arrow" href="https://pytorch.org/resources">View Resources</a>
        </div>
      </div>
    </div>
  </div>

  <footer class="site-footer">
    <div class="container footer-container">
      <div class="footer-logo-wrapper">
        <a href="https://pytorch.org/" class="footer-logo"></a>
      </div>

      <div class="footer-links-wrapper">
        <div class="footer-links-col">
          <ul>
            <li class="list-title"><a href="https://pytorch.org/">PyTorch</a></li>
            <li><a href="https://pytorch.org/get-started">Get Started</a></li>
            <li><a href="https://pytorch.org/features">Features</a></li>
            <li><a href="https://pytorch.org/ecosystem">Ecosystem</a></li>
            <li><a href="https://pytorch.org/blog/">Blog</a></li>
            <li><a href="https://github.com/pytorch/pytorch/blob/master/CONTRIBUTING.md">Contributing</a></li>
          </ul>
        </div>

        <div class="footer-links-col">
          <ul>
            <li class="list-title"><a href="https://pytorch.org/resources">Resources</a></li>
            <li><a href="https://pytorch.org/tutorials">Tutorials</a></li>
            <li><a href="https://pytorch.org/docs/stable/index.html">Docs</a></li>
            <li><a href="https://discuss.pytorch.org" target="_blank">Discuss</a></li>
            <li><a href="https://github.com/pytorch/pytorch/issues" target="_blank">Github Issues</a></li>
            <li><a href="https://pytorch.org/assets/brand-guidelines/PyTorch-Brand-Guidelines.pdf" target="_blank">Brand Guidelines</a></li>
          </ul>
        </div>

        <div class="footer-links-col follow-us-col">
          <ul>
            <li class="list-title">Stay Connected</li>
            <li>
              <div id="mc_embed_signup">
                <form
                  action="https://twitter.us14.list-manage.com/subscribe/post?u=75419c71fe0a935e53dfa4a3f&id=91d0dccd39"
                  method="post"
                  id="mc-embedded-subscribe-form"
                  name="mc-embedded-subscribe-form"
                  class="email-subscribe-form validate"
                  target="_blank"
                  novalidate>
                  <div id="mc_embed_signup_scroll" class="email-subscribe-form-fields-wrapper">
                    <div class="mc-field-group">
                      <label for="mce-EMAIL" style="display:none;">Email Address</label>
                      <input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL" placeholder="Email Address">
                    </div>

                    <div id="mce-responses" class="clear">
                      <div class="response" id="mce-error-response" style="display:none"></div>
                      <div class="response" id="mce-success-response" style="display:none"></div>
                    </div>    <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->

                    <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_75419c71fe0a935e53dfa4a3f_91d0dccd39" tabindex="-1" value=""></div>

                    <div class="clear">
                      <input type="submit" value="" name="subscribe" id="mc-embedded-subscribe" class="button email-subscribe-button">
                    </div>
                  </div>
                </form>
              </div>

            </li>
          </ul>

          <div class="footer-social-icons">
            <a href="https://www.facebook.com/pytorch" target="_blank" class="facebook"></a>
            <a href="https://twitter.com/pytorch" target="_blank" class="twitter"></a>
            <a href="https://www.youtube.com/pytorch" target="_blank" class="youtube"></a>
          </div>
        </div>
      </div>
    </div>
  </footer>

  <div class="cookie-banner-wrapper">
  <div class="container">
    <p class="gdpr-notice">To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: <a href="https://www.facebook.com/policies/cookies/">Cookies Policy</a>.</p>
    <img class="close-button" src="../../_static/images/pytorch-x.svg">
  </div>
</div>

  <!-- End Footer -->

  <!-- Begin Mobile Menu -->

  <div class="mobile-main-menu">
    <div class="container-fluid">
      <div class="container">
        <div class="mobile-main-menu-header-container">
          <a class="header-logo" href="https://pytorch.org/" aria-label="PyTorch"></a>
          <a class="main-menu-close-button" href="#" data-behavior="close-mobile-menu"></a>
        </div>
      </div>
    </div>

    <div class="mobile-main-menu-links-container">
      <div class="main-menu">
        <ul>
          <li>
            <a href="https://pytorch.org/get-started">Get Started</a>
          </li>

          <li>
            <a href="https://pytorch.org/features">Features</a>
          </li>

          <li>
            <a href="https://pytorch.org/ecosystem">Ecosystem</a>
          </li>

          <li>
            <a href="https://pytorch.org/mobile">Mobile</a>
          </li>

          <li>
            <a href="https://pytorch.org/hub">PyTorch Hub</a>
          </li>

          <li>
            <a href="https://pytorch.org/blog/">Blog</a>
          </li>

          <li>
            <a href="https://pytorch.org/tutorials">Tutorials</a>
          </li>

          <li class="active">
            <a href="https://pytorch.org/docs/stable/index.html">Docs</a>
          </li>

          <li>
            <a href="https://pytorch.org/resources">Resources</a>
          </li>

          <li>
            <a href="https://github.com/pytorch/pytorch">Github</a>
          </li>
        </ul>
      </div>
    </div>
  </div>

  <!-- End Mobile Menu -->

  <script type="text/javascript" src="../../_static/js/vendor/anchor.min.js"></script>

  <script type="text/javascript">
    $(document).ready(function() {
      mobileMenu.bind();
      mobileTOC.bind();
      pytorchAnchors.bind();
      sideMenus.bind();
      scrollToAnchor.bind();
      highlightNavigation.bind();
      mainMenuDropdown.bind();
      filterTags.bind();

      // Remove any empty p tags that Sphinx adds
      $("[data-tags='null']").remove();

      // Add class to links that have code blocks, since we cannot create links in code blocks
      $("article.pytorch-article a span.pre").each(function(e) {
        $(this).closest("a").addClass("has-code");
      });
    })
  </script>
</body>
</html>